#!/usr/bin/env bash

##########
## 
##
##########

## /functions ##

# Return pid for port
# @param param1 required port
function pidForPort {
	restartPid=`lsof -i:$1 | sed -n "2p" | awk '{print $2}'`
	echo ${restartPid}
}

# Return line number which string display
# @param param1 searchStr
# @param param2 file
function lineNum {
	if [ $# != 2 ]
	then
		echo $1
		echo "useage $0 serachStr file"
	else
		result=`grep -n $1 $2 |sed -n '1p'| awk -F ':' '{ print \$1 }'`
		echo ${result}
	fi
}

# Return standby tomcat port
# @param param1 port1
# @param param2 port2
# @param param3 file
function standbyTomcatPort {
	port1_line_num=`lineNum $1 $3`
	port2_line_num=`lineNum $2 $3`

	if [ ${port1_line_num} \> ${port2_line_num} ] 
	then
		echo $1
	else
		echo $2
	fi
}

function currentTomcatPort {
	port1_line_num=`lineNum $1 $3`
	port2_line_num=`lineNum $2 $3`

	if [ ${port1_line_num} \> ${port2_line_num} ] 
	then
		echo $2
	else
		echo $1
	fi	
}

# tomcat path for pid
# param1 required pid
function tomcatPathForPid {
	tomcat_path=`ps -ef | grep $1 | tr -s ' ' '\\n' | grep 'Dcatalina.home' | tr -s '=' '\\n' | sed -n "2p"`
	echo ${tomcat_path}
}

# tomcat path for pid
# param1 required pid
function tomcatPathForPid {
	tomcat_path=`ps -ef | grep $1 | tr -s ' ' '\\n' | grep 'Dcatalina.home' | tr -s '=' '\\n' | sed -n "2p"`
	echo ${tomcat_path}
}

# replace string in file
# @param param1 search string
# @param param2 replace string
# @param param3 search file
function replaceStrInFile {
	 sed -i -e "s/$1/$2/g" $3
}

## /functions ##

## 处理逻辑 ##
if [ $# != 5 ] 
then
	echo "useage $0 port1 port2 nginxFile warFile path"
else
	# 检查参数
	if [ ! -f "$4" ]; then
		echo "war file not exists, $4"
		exit 0
	fi

	if [ ! -f "$3" ]; then
		echo "nginx config file not exists, $3"
		exit 0
	fi

	# 1. 升级备用机器


	# 1.1 通过端口查找当前使用tomcat的相关信息
	current_tomcat_port=`currentTomcatPort $1 $2 $3`
	standby_tomcat_port=`standbyTomcatPort $1 $2 $3`
	current_tomcat_pid=`pidForPort ${current_tomcat_port}`
	current_tomcat_path=`tomcatPathForPid ${current_tomcat_pid}`
	standby_tomcat_config_file="${current_tomcat_path}/bin/standbyserver.path"
	echo "standby_tomcat_config_file:"${standby_tocmat_config_file}
	# 1.2 读取备用机的路径(已写入主tomact下的指定文件 bin/standbyserver.path) 并进行校验
	if [ ! -f "${standby_tomcat_config_file}" ]; then
		echo “current tomcat\' bin/directory/ missing standby tocmat config（need standbyserver.path）”
		exit 0
	fi

	standby_tomcat_path_config=`grep 'standby.server.path' ${standby_tomcat_config_file}`
	echo ${standby_tomcat_path_config}
	if [ -z "${standby_tomcat_path_config}" ]; then 
		echo "standby tomcat server config does not exists in ${standby_tomcat_path_config}, please config it like this==> stand
by.server.path:xxxxxxx"
		exit 0;
	fi

	standby_tomcat_path=`echo ${standby_tomcat_path_config}|awk -F ':' '{print \$2}'`
	echo ${standby_tomcat_path}
	if [ ! -d "${standby_tomcat_path}" ]; then 
		echo "standby tomcat does not exists, ${standby_tomcat_path}"
		exit 0;
	fi

	# 1.3 清空旧文件、复制war文件到备用机 并启动standyTomcat
	rm -rf ${standby_tomcat_path}/bin/catalina.pid
 	rm -rf ${standby_tomcat_path}/logs/*
 	rm -rf ${standby_tomcat_path}/webapps/*
 	cp $4  ${standby_tomcat_path}/webapps/$5.war
	nohup ${standby_tomcat_path}/bin/startup.sh & 

	# 1.4 切换主tomcat和备用机(修改nginx配置文件)
	replaceStrInFile ${standby_tomcat_port} "_TMP_PORT" $3
 	replaceStrInFile ${current_tomcat_port} ${standby_tomcat_port} $3
 	replaceStrInFile "_TMP_PORT" ${current_tomcat_port} $3

 	# 1.5 写入下次使用tomcat的路径到刚启用tomcat的指定文件
	echo standby.server.path:${current_tomcat_path} > "${standby_tomcat_path}/bin/standbyserver.path"

	# 1.6 重新加在nginx配置
	sudo /usr/local/nginx/sbin/nginx -s reload

	# 1.7 kill 原tomcat
	kill -9 ${current_tomcat_pid}
fi
## /处理逻辑 ##
